/**
 * 如果要定义一个函数是用作工具类，命名方式为 methodNameUtil，然后注册到commonUtils对象中
 */
import AllEmotions from '../../static/emotions/all.json';
import {save, read, clear} from '../storage/session';

const CURRENT_PROJECT = "current_project";
const CURRENT_LOGIN_INFO = 'login/CURRENT_LOGIN_INFO'
/**
 * 根据属性名称和属性值从数组中删除
 * @param array
 * @param proprety
 * @param value
 * @returns {*}
 */
function removeFromArrayByPropertyVue(array, proprety, value) {
  if (array.length === 0) return array
  const result = _.remove(array, function (data) {
    if (data[proprety] !== value) {
      return data
    }
  })
  return result
}

function emos(emoMsg) {
  var emoPath, transMsg;
  var reg = /\[(.*?)\]/gm;
  transMsg = emoMsg.replace(reg, function (match) {
    for (var i = 0, len = AllEmotions.length; i < len; i++) {
      if (AllEmotions[i].value === match) {
        emoPath = AllEmotions[i].url;
        return '<img width="20" height="20" src="' + emoPath + '" style="vertical-align:middle;" />'
      }
    }
    return match;
  });
  return transMsg;
}

/**
 * 字典过滤
 * @param id
 * @param dictList 字典组
 * @returns {*}
 */
function dictFilter(id, dictList) {
  if (dictList == undefined) {
    return {};
  }
  var len = dictList.length;
  for (var i = 0; i < len; i++) {
    var dict = dictList[i];
    if (dict.id == id) {
      return dict;
    } else if (dict.child && dict.child.length > 0) {
      return dictFilter(id, dict.child);
    }
  }
}


const MENU_LIST = 'MENU_LIST';
var menu_code_list = new Array();
var menu_list = {};

/**
 * 分析菜单tree
 * @param menuList
 */
function ckMenu(menuList) {
  if (menuList instanceof Array) {
    if (menuList.length > 0)
      for (var i = 0; i < menuList.length; i++) {
        var menu = menuList[i];
        if (menu == undefined)
          continue;
        if (menu.child && menu.child.length > 0) {
          for (var c = 0; c < menu.child.length; c++) {
            var menu_ = menu.child[c];
            if (menu_ != undefined)
              ckMenu(menu_);
          }
        }
        menu_code_list.push(menu.code);
        menu_list[menu.code] = menu;
      }

  } else {
    if (menuList.code) {
      menu_code_list.push(menuList.code);
      menu_list[menuList.code] = menuList;
    }

    if (menuList.child) {
      ckMenu(menuList.child);
    }
  }
}

/**
 * 判断UI权限
 * @param code
 */
function ckUIPower(code) {
  var json = read(MENU_LIST);
  if (json == undefined || json == '') {
    return false;
  }
  menu_code_list = new Array();
  var menuList = JSON.parse(json);
  if (menuList && menuList.length > 0) {
    ckMenu(menuList);
  }
  // return menu_code_list.indexOf(code) >= 0;
  return true;
}

/***
 * 返回当前路由及父菜单
 * @param code 对应router的name
 * @return List<menu>
 */
function breadcrumbRouter(code) {
  var menuList = new Array();
  var l = (code.length - 1) / 2;
  for (var i = 1; i <= l; i++) {
    var m = menu_list[code.substring(0, 1 + i * 2)];
    if (m)
      menuList.push(m);
  }
  return menuList;
}

function getCurrenrProject() {
  var json = read(CURRENT_PROJECT);
  if (json != undefined && json != '') {
    return JSON.parse(json);
  }
  return {};
}

/**
 * 获取当前用户信息
 * @returns {{}}
 */
function getCurrenrUserInfo() {
  var json = read(CURRENT_LOGIN_INFO);
  if (json != undefined && json != '') {
    return JSON.parse(json).currentUser.user;
  }
  return {};
}
/**
 * 获取当前用户信息
 * @returns {{}}
 */
function getRoleCodeList() {
  return getCurrenrUserInfo().roleCodeList;
}

function setCurrenrProject(project) {
  save(CURRENT_PROJECT, JSON.stringify(project));
}

function clearCurrenrProject() {
  clear(CURRENT_PROJECT);
}

export default {
  removeFromArrayByPropertyVue,
  dictFilter,
  ckMenu,
  ckUIPower,
  breadcrumbRouter,
  emos,
  getCurrenrProject, setCurrenrProject, clearCurrenrProject, getCurrenrUserInfo,getRoleCodeList
}

